package com.dmap.admin.controller.rpc;

import com.dmap.admin.bo.company.CompanyAPPInfo;
import com.dmap.admin.po.Company;
import com.dmap.admin.service.rpc.CompanyInfoService;
import com.dmap.admin.vo.backgroundv2.CompanyInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * \* User: helei
 * \* Date: 25/05/18
 * \* Description:
 * \
 */
@Api(value = "个人数据服务系统-rpc公司信息管理", description = "background/rpc公司信息管理")
@Controller
@RequestMapping(value = "/rpc/background/pmpd-admin-server/v2/companyInfo")
public class CompanyInfoController {

    @Resource
    private CompanyInfoService companyInfoService;

    @ResponseBody
    @ApiOperation(value = "根据projectId查询公司信息",
            notes = "1. 必填： projectId; " +
                    "2. 返回公司信息； ")
    @RequestMapping(value = "select/selectByProjectId", method = RequestMethod.GET)
    public CompanyInfoVo selectByProjectId(@RequestParam String projectId) {
        CompanyInfoVo companyInfoVo = new CompanyInfoVo();
        if (StringUtils.isBlank(projectId)) {
            return companyInfoVo;
        }
        List<Company> list = companyInfoService.selectByProjectId(projectId);
        if (CollectionUtils.isNotEmpty(list)) {
            companyInfoVo.setId(list.get(0).getId().toString());
            companyInfoVo.setNumber(list.get(0).getNumber());
        }
        return companyInfoVo;
    }


    @ResponseBody
    @ApiOperation(value = "根据app编号查询app的信息和app所属公司的信息",
            notes = "调用：knowledge模块获取app和公司信息")
    @RequestMapping(value = "select/selectCompanyAppInfoByProjectId", method = RequestMethod.GET)
    public CompanyAPPInfo selectCompanyAppInfo(@RequestParam String appNumber) {
        if (StringUtils.isBlank(appNumber)) {
            return new CompanyAPPInfo();
        }
        return companyInfoService.selectCompanyAPPInfo(appNumber);
    }

    @ResponseBody
    @ApiOperation(value = "根据CompanyId查询公司下projectId信息",
            notes = "1. 必填： projectId; " +
                    "2. 返回公司下projectId信息； ")
    @RequestMapping(value = "select/selectByCompanyId", method = RequestMethod.GET)
    public List<String> selectByCompanyId(@RequestParam Long companyId) {
        List<String> list = new ArrayList<>();
        if (companyId == null) {
            return list;
        }
        list = companyInfoService.selectByCompanyId(companyId);
        Set<String> companyProjectSet = new HashSet<>(list);
        return new ArrayList<>(companyProjectSet);
    }

}